﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mahanet.Abyss.PathCalculation;

namespace motion
{
	public partial class CameraCalibrationWindow : Form
	{
		private CrossLayer m_Camera1CrossLayer;
		private CrossLayer m_Camera2CrossLayer;

		public CameraCalibrationWindow(Camera camera1, Camera camera2)
		{
			InitializeComponent();

			cameraWindow.Camera = camera1;
			cameraWindow2.Camera = camera2;

			m_Camera1CrossLayer = new CrossLayer(cameraWindow);
			m_Camera2CrossLayer = new CrossLayer(cameraWindow2);

			SetCenterText();
		}

		protected override void OnResize(EventArgs e)
		{
			base.OnResize(e);

			SetCenterText();
		}

		private void SetCenterText()
		{
			double x1 = cameraWindow.Width / 2;
			double y1 = cameraWindow.Height / 2;
			double x2 = cameraWindow2.Width / 2;
			double y2 = cameraWindow2.Height / 2;

			m_TextBoxCamera1Center.Text = string.Format("({0}, {1})", x1, y1);
			m_TextBoxCamera2Center.Text = string.Format("({0}, {1})", x2, y2);
		}

		private void cameraWindow_MouseClick(object sender, MouseEventArgs e)
		{
			Coordinate2D center = new Coordinate2D(cameraWindow.Width / 2, cameraWindow.Height / 2);
			Coordinate2D secondEdge = new Coordinate2D(e.X, e.Y);
			m_Camera1CrossLayer.ClearCrosses();
			m_Camera1CrossLayer.AddCross(secondEdge, Color.Red);
			m_TextBoxCamera1SecondEdge.Text = "(" + e.X + "," + e.Y + ")";
			m_TextBoxCamera1Distance.Text = secondEdge.DistanceTo(center).ToString();
		}

		private void cameraWindow2_MouseClick(object sender, MouseEventArgs e)
		{
			Coordinate2D center = new Coordinate2D(cameraWindow2.Width / 2, cameraWindow2.Height / 2);
			Coordinate2D secondEdge = new Coordinate2D(e.X, e.Y);
			m_Camera2CrossLayer.ClearCrosses();
			m_Camera2CrossLayer.AddCross(secondEdge, Color.Red);
			m_TextBoxCamera2SecondEdge.Text = "(" + e.X + "," + e.Y + ")";
			m_TextBoxCamera2Distance.Text = secondEdge.DistanceTo(center).ToString();
		}
	}
}
